{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "%matplotlib inline\n",
    "import _init_paths\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "from ult.config import cfg\n",
    "\n",
    "import _init_paths\n",
    "import pickle\n",
    "import json\n",
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "import sys\n",
    "\n",
    "CLASSES = ('__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus','train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter','bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack','umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite','baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl','banana', 'apple', 'sandwich', 'orange', 'broccoli','carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table','toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven','toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier','toothbrush')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Detection = pickle.load( open( cfg.ROOT_DIR + \"/demo/HOI_Detection.pkl\", \"rb\" ) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_id = 'Djokovic_0001.png'\n",
    "\n",
    "cc = plt.get_cmap('hsv', lut=6)\n",
    "dpi = 80\n",
    "\n",
    "im_file = cfg.ROOT_DIR + '/demo/' + image_id\n",
    "im_data = plt.imread(im_file)\n",
    "height, width, nbands = im_data.shape\n",
    "figsize = width / float(dpi), height / float(dpi)\n",
    "fig = plt.figure(figsize=figsize)\n",
    "ax = fig.add_axes([0, 0, 1, 1])\n",
    "ax.axis('off')\n",
    "ax.imshow(im_data, interpolation='nearest')\n",
    "\n",
    "HO_dic = {}\n",
    "HO_set = set()\n",
    "count = 0\n",
    "\n",
    "\n",
    "for ele in Detection:\n",
    "    if (ele['image_id'] == image_id):\n",
    "        action_count = -1\n",
    "        H_box = ele['person_box'] \n",
    "        \n",
    "        if tuple(H_box) not in HO_set:\n",
    "            HO_dic[tuple(H_box)] = count\n",
    "            HO_set.add(tuple(H_box))\n",
    "            count += 1 \n",
    "        \n",
    "        show_H_flag = 0\n",
    "        \n",
    "        if ele['smile'][4] > 0.5:\n",
    "            ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35,\n",
    "            'smile, ' + \"%.2f\" % ele['smile'][4] ,\n",
    "            bbox=dict(facecolor=cc(HO_dic[tuple(H_box)])[:3], alpha=0.5),\n",
    "            fontsize=16, color='white')\n",
    "            action_count += 1 \n",
    "            show_H_flag = 1\n",
    "\n",
    "        if ele['stand'][4] > 0.5:\n",
    "            ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35,\n",
    "            'stand, ' + \"%.2f\" % ele['stand'][4] ,\n",
    "            bbox=dict(facecolor=cc(HO_dic[tuple(H_box)])[:3], alpha=0.5),\n",
    "            fontsize=16, color='white')\n",
    "            action_count += 1             \n",
    "            show_H_flag = 1\n",
    "\n",
    "        if ele['run'][4] > 0.5:\n",
    "            ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35,\n",
    "            'run, ' + \"%.2f\" % ele['run'][4] ,\n",
    "            bbox=dict(facecolor=cc(HO_dic[tuple(H_box)])[:3], alpha=0.5),\n",
    "            fontsize=16, color='white')\n",
    "            action_count += 1  \n",
    "            show_H_flag = 1\n",
    "\n",
    "        if ele['walk'][4] > 0.5:\n",
    "            ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35,\n",
    "            'walk, ' + \"%.2f\" % ele['walk'][4] ,\n",
    "            bbox=dict(facecolor=cc(HO_dic[tuple(H_box)])[:3], alpha=0.5),\n",
    "            fontsize=16, color='white')\n",
    "            action_count += 1  \n",
    "            show_H_flag = 1\n",
    "        \n",
    "        for action_key, action_value in ele.iteritems():\n",
    "            if (action_key.split('_')[-1] != 'agent') and action_key != 'image_id' and action_key != 'person_box':\n",
    "                if (not np.isnan(action_value[0])) and (action_value[5] > 0.05):\n",
    "                    O_box = action_value[:4]\n",
    "                    \n",
    "                    action_count += 1\n",
    "                    \n",
    "                    if tuple(O_box) not in HO_set:\n",
    "                        HO_dic[tuple(O_box)] = count\n",
    "                        HO_set.add(tuple(O_box))\n",
    "                        count += 1      \n",
    "                \n",
    "                    ax.add_patch(\n",
    "                    plt.Rectangle((H_box[0], H_box[1]),\n",
    "                                  H_box[2] - H_box[0],\n",
    "                                  H_box[3] - H_box[1], fill=False,\n",
    "                                  edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=3)\n",
    "                    )\n",
    "                    text = action_key.split('_')[0] + ' ' + CLASSES[np.int(action_value[4])] + ', ' + \"%.2f\" % action_value[5]\n",
    "\n",
    "                    ax.text(H_box[0] + 10, H_box[1] + 25 + action_count * 35,\n",
    "                        text,\n",
    "                        bbox=dict(facecolor=cc(HO_dic[tuple(O_box)])[:3], alpha=0.5),\n",
    "                        fontsize=16, color='white')\n",
    "\n",
    "                    ax.add_patch(\n",
    "                    plt.Rectangle((O_box[0], O_box[1]),\n",
    "                                  O_box[2] - O_box[0],\n",
    "                                  O_box[3] - O_box[1], fill=False,\n",
    "                                  edgecolor=cc(HO_dic[tuple(O_box)])[:3], linewidth=3)\n",
    "                    )\n",
    "                    ax.set(xlim=[0, width], ylim=[height, 0], aspect=1)\n",
    "        if show_H_flag == 1:\n",
    "            ax.add_patch(\n",
    "            plt.Rectangle((H_box[0], H_box[1]),\n",
    "                          H_box[2] - H_box[0],\n",
    "                          H_box[3] - H_box[1], fill=False,\n",
    "                          edgecolor=cc(HO_dic[tuple(H_box)])[:3], linewidth=3)\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (TF_huck)",
   "language": "python",
   "name": "tf_huck"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
